//
//  LogonViewController.m
//  一点互助
//
//  Created by 张泊秋 on 16/8/20.
//  Copyright © 2016年 zhangboqiu. All rights reserved.
//

#import "LogonViewController.h"
#import "underlineTextField.h"
#import <Masonry/Masonry.h>
#import "ResponseManager.h"
#import "ResponseManager+LogonInfo.h"
#import "MyViewController.h"
#import "FBProgressView.h"

@interface LogonViewController (){
    UITextField *phone;
    UITextField *passWord;
    UITextField *authCode;
    underlineTextField *referrer;
    UIButton *logonButton;
    UIButton *sendAuthCode;
}

@end

@implementation LogonViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupBackItem];
    self.view.backgroundColor = RGBCOLOR(241.0f, 241.0f, 241.0f, 1);
    [self loadData];
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(logonColor) name:UITextFieldTextDidChangeNotification object:nil];
    
    
    
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = NO;
}

- (void)loadData {
    
    UILabel *label = [[UILabel alloc]init];
    label.text = @"推荐人";
    label.font = [UIFont systemFontOfSize:12];
    label.textAlignment = NSTextAlignmentRight;
    [self.view addSubview:label];

    
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(15);
        make.top.mas_equalTo(94);
        make.width.mas_equalTo(40);
        make.height.mas_equalTo(20);
    }];
    
    referrer = [[underlineTextField alloc]init];
    referrer.textAlignment = NSTextAlignmentCenter;
    referrer.font = [UIFont systemFontOfSize:12];
    
    
    [self.view addSubview:referrer];
    [referrer mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(label.mas_right).with.mas_offset(0);
        make.top.mas_equalTo(94);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(20);
    }];
    
    UILabel *remark = [[UILabel alloc]init];
    remark.font = [UIFont systemFontOfSize:12];
    remark.text = @"(选填)";
    [self.view addSubview:remark];
    [remark mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(referrer.mas_right).with.mas_offset(0);
        make.top.mas_equalTo(94);
        make.width.mas_equalTo(35);
        make.height.mas_equalTo(20);
        
}];
    UIView *view = [[UIView alloc]init];
    view.frame = CGRectMake(0, 0, 110, 35);
    
    sendAuthCode = [UIButton buttonWithType:UIButtonTypeCustom];
    sendAuthCode.frame = CGRectMake(0, 0, 90, 35);
    sendAuthCode.layer.cornerRadius = 10;
    sendAuthCode.layer.masksToBounds = YES;
    sendAuthCode.layer.borderWidth = 1;
    sendAuthCode.layer.borderColor = [UIColor colorWithRed:170.0f/255.0f green:170.0f/255.0f blue:170.0f/255.0f alpha:1].CGColor;
    [sendAuthCode setTitle:@"获取验证码" forState:UIControlStateNormal];
    sendAuthCode.titleLabel.font = [UIFont systemFontOfSize:14];
    [sendAuthCode setTitleColor:RGBCOLOR(170.0f, 170.0f, 170.0f, 1) forState:UIControlStateNormal];
    [view addSubview: sendAuthCode];
    
    
    
    
    
    
    phone = [self memberTextFieldText:@"请输入手机号" image:@"登录注册-手机" label:nil];
    [self.view addSubview:phone];
    phone.rightView = view;
    phone.rightViewMode = UITextFieldViewModeAlways;
    [phone mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(referrer.mas_bottom).with.mas_offset(5);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(50);
        
    }];
    
    passWord = [self memberTextFieldText:@"请输入密码" image:@"登录注册-钥匙" label:nil];
    passWord.secureTextEntry = YES;
    [self.view addSubview:passWord];
    [passWord mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(phone.mas_bottom).with.mas_offset(1);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(50);
    }];
    
    authCode = [self memberTextFieldText:@"请输入验证码" image:@"登录注册-验证码" label:nil];
    [self.view addSubview:authCode];
    [authCode mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(passWord.mas_bottom).with.mas_offset(1);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(50);
    }];
    
    logonButton= [[UIButton alloc]init];
    logonButton.backgroundColor = RGBCOLOR(170.0f, 170.0f, 170.0f, 1);
    [logonButton setTitle:@"注册" forState:UIControlStateNormal];
    [logonButton setTitleColor:RGBCOLOR(205.0f, 205.0f, 205.0f, 1) forState:UIControlStateHighlighted];
    logonButton.titleLabel.textColor = [UIColor whiteColor];
    logonButton.titleLabel.font = [UIFont systemFontOfSize:15];
    logonButton.layer.cornerRadius = 5;
    [self.view addSubview:logonButton];
    
    [logonButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(authCode.mas_bottom).with.offset(30);
        make.height.mas_equalTo(50);
        
    }];
    
}

- (void)logonColor {
    if (phone.text.length >= 1) {
      sendAuthCode.layer.borderColor = [UIColor colorWithRed:89.0f/255.0f green:202.0f/255.0f blue:195.0f/255.0f alpha:1].CGColor;
        [sendAuthCode setTitleColor:BLUE_COLOR forState:UIControlStateNormal];
        [sendAuthCode addTarget:self action:@selector(sendAuthCodeButtonTaped:) forControlEvents:UIControlEventTouchUpInside];
    }else {
        sendAuthCode.layer.borderColor = [UIColor colorWithRed:170.0f/255.0f green:170.0f/255.0f blue:170.0f/255.0f alpha:1].CGColor;
        [sendAuthCode setTitleColor:RGBCOLOR(170.0f, 170.0f, 170.0f, 1) forState:UIControlStateNormal];
        
        
    }
    if (phone.text.length >= 1 && passWord.text.length >= 1 && authCode.text.length >= 1) {
        logonButton.backgroundColor = BLUE_COLOR;
        [logonButton addTarget:self action:@selector(logonButtonTaped:) forControlEvents:UIControlEventTouchUpInside];
    }else {
        logonButton.backgroundColor = RGBCOLOR(170.0f, 170.0f, 170.0f, 1);
    }
}

- (void)logonButtonTaped:(UIButton *)button {
    BOOL correct = [self isPhoneNumberOfString:phone.text];
    __weak typeof(self) myself = self;
       if (correct == YES) {
        [FBProgressView showWithMessage:nil];
        [ResponseManager getUserInfoLogonUserName:phone.text passWord:passWord.text completionHandler:^(FBResponse *response) {
            if (response.success) {
                 [FBProgressView hide];
                [[EMClient sharedClient] asyncRegisterWithUsername:phone.text password:passWord.text success:^{
                    [myself addAlertControllerWithTitle:@"提示" message:@"注册成功" actionNum:1 style:UIAlertControllerStyleAlert handler:nil];
                    NSLog(@"注册成功");
                    [myself.navigationController popToViewController:self.navigationController.viewControllers[1] animated:YES];
                } failure:^(EMError *aError) {
                    [myself addAlertControllerWithTitle:@"提示" message:aError.debugDescription actionNum:1 style:UIAlertControllerStyleAlert handler:nil];
                    NSLog(@"注册失败");
                }];
//
//                NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
//                [userDefaults setObject:response.resultValue[@"id"] forKey:@"userid"];
            }else {
                [FBProgressView hide];
                [self addAlertControllerWithTitle:@"提示" message:                response.resultDesc actionNum:1 style:UIAlertControllerStyleAlert handler:nil];
            }
        }];
        
    }else {
        [self addAlertControllerWithTitle:@"提示" message:@"请输入正确手机号" actionNum:1 style:UIAlertControllerStyleAlert handler:nil];
    }

    
}

- (void)sendAuthCodeButtonTaped:(UIButton *)button {
    
    BOOL correct = [self isPhoneNumberOfString:phone.text];
    if (correct == YES) {
        
    }else {
        [self addAlertControllerWithTitle:@"提示" message:@"请输入正确手机号" actionNum:1 style:UIAlertControllerStyleAlert handler:nil];
    }
    
}

-(BOOL)isPhoneNumberOfString:(NSString *)phoneString{
    
    NSMutableString *str3=[[NSMutableString alloc]init];
    
    int count=0,sum = 0;
    
    NSString *str1=[phoneString substringToIndex:1];
    
    if ([str1 isEqualToString:@"1"]==YES) {
        
        ++sum;
//        NSLog(@"sum = %d",sum);
        [str3 appendString:@"第一个号码为:1"];
        
        // BOOL ret=[[str3 substringWithRange:NSMakeRange(0, 1)]isEqualToString:@"1"];
        
    }
    
    //
    
    for (NSUInteger i=0; i<[phoneString length]; i++) {
        
        unichar c=[phoneString characterAtIndex:i];
        
//        int set=(int)c-48;
        
        if(c>='0'&&c<='9'){
            
            ++count;
            
//            NSLog(@"%d",count);
            
        }
        
    }
    
//    NSLog(@"(7)手机号码全为数字的位数：%d %@",count,str3);
//    NSLog(@"%d",sum);
//    NSLog(@"如何号码符合返回1：如何号码不符合返回0：%d",(count==11&&sum==1));
    
    return (count==11&&sum==1);
    
}





@end
